import {create, findMany, updateOne, deleteOne} from "../services/dict";

export default {
  namespace: 'dict',
  state: {},
  effects: {
    * create({payload}, {call, put}) {
      const response = yield call(create, payload);
      console.log('responsee', response);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findMany({payload}, {call, put}) {
      const response = yield call(findMany, payload);
      const {type} = payload;
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        const payload = {};
        payload[`${type}Dict`] = result;
        yield put({
          type: 'onDataLoad',
          payload
        });

        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * updateOne({payload}, {call, put}) {
      const response = yield call(updateOne, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * deleteOne({payload}, {call, put}) {
      const response = yield call(deleteOne, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }
      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    }
  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    },
    onNotify(state, {payload}) {
      console.log('notifyyy', payload);
      state._notify = payload;
      return {...state, ...payload};
    }
  }

};
